<?
    include "../../common.php" ;
    $opt = $_POST['opt'] ; 
    $arr = [] ;
    if( $opt == 'getList' ){
        $page = $_POST['page'] ;
        $perPage = $_POST['perPage'] ;
        $pageSt = ( $page - 1 ) * $perPage ; 
        $sql = "SELECT * FROM `ptype` WHERE is_banner = 1 ORDER BY id ASC LIMIT {$pageSt} , {$perPage} " ;
        $res = mysqli_query($link,$sql);
        while( $one = mysqli_fetch_assoc($res) ){
            $arr[] = $one ; 
        }
    }
    if( $opt == 'addType' ){
        $typeName = $_POST['TypeName'] ;
        $sql = "INSERT INTO `ptype`(`typeName`) VALUES ('{$typeName}')" ;
        $res = mysqli_query($link,$sql);
        if( $res ){
            $arr['status'] = 1 ;
            $arr['info'] = '添加成功!' ;
        } else {
            $arr['status'] = 0 ;
            $arr['info'] = '操作失败!' ;
        }
    }
    if( $opt == 'updType' ){ 
        $typeName = $_POST['typeName'] ;
        $myid = $_POST['myid'] ;
        $sql = "UPDATE `ptype` SET `typeName`='{$typeName}' WHERE id = {$myid}" ;
        $res = mysqli_query($link,$sql);
        if( $res ){
            $arr['status'] = 1 ;
            $arr['info'] = '修改成功!' ;
        } else {
            $arr['status'] = 0 ;
            $arr['info'] = '操作失败!' ;
        }
    }
    if( $opt == 'del' ){
        $myid = $_POST['myid'] ;
        $sql = "DELETE FROM `ptype` WHERE id = {$myid}" ;
        $res = mysqli_query($link,$sql);
        if( $res ){
            $arr['status'] = 1 ;
            $arr['info'] = '删除成功!' ;
        } else {
            $arr['status'] = 0 ;
            $arr['info'] = '操作成功!' ;
        }
    }
    if( $opt == 'getPageNum' ){
        $perPage = $_POST['perPage'] ;
        $sql = "SELECT COUNT(*) AS c FROM `ptype` WHERE is_banner = 1 ORDER BY id ASC" ;
        $res = mysqli_query($link,$sql);
        while( $one = mysqli_fetch_assoc($res) ){
            $pageNum = ceil( $one['c'] / $perPage ) ; 
            $arr['totalPage'] = $pageNum ; 
        }
    }
    if( $opt == 'getUpdType' ){
        $myid = $_POST['myid'] ;
        $sql = "SELECT * FROM `ptype` WHERE id = {$myid}" ;
        $res = mysqli_query($link,$sql);
        if( $one = mysqli_fetch_assoc($res) ){
            $arr = $one ; 
        }
    }
    echo json_encode($arr);
?>